package com.hiep.connect.client.view;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionModel;
import com.hiep.connect.client.presenter.UserSearchResultPresenter.UserSearchResultDisplay;
import com.hiep.connect.shared.MyUser;

public class UserSearchResultView extends Composite implements
		UserSearchResultDisplay {

	private Button addButton;

	private MyUser user;
	private List<MyUser> searchResult;

	CellTable<MyUser> cellTable;

	/**
	 * The pager used to change the range of data.
	 */
	SimplePager pager;

	public UserSearchResultView(MyUser user, List<MyUser> searchResult) {
		this.user = user;
		this.searchResult = searchResult;
		initGui();
	}

	private void initGui() {
		initCellTable();

		addButton = new Button("Add friend");
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.setSpacing(15);
		buttonPanel.add(addButton);

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.add(cellTable);
		vPanel.add(buttonPanel);

		DecoratorPanel panel = new DecoratorPanel();
		panel.add(vPanel);

		initWidget(panel);
	}

	public void initCellTable() {
		// Create a CellTable.

		// Set a key provider that provides a unique key for each contact. If
		// key is
		// used to identify contacts when fields (such as the name and address)
		// change.
		cellTable = new CellTable<MyUser>();
		cellTable.setWidth("100%", true);

		// Create a data provider.
		ListDataProvider<MyUser> dataProvider = new ListDataProvider<MyUser>();

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(cellTable);

		// Add the data to the data provider, which automatically pushes it to
		// the
		// widget.
		List<MyUser> list = dataProvider.getList();
		for (MyUser u : searchResult) {
			list.add(u);
		}

		// Attach a column sort handler to the ListDataProvider to sort the
		// list.
		ListHandler<MyUser> sortHandler = new ListHandler<MyUser>(searchResult);
		cellTable.addColumnSortHandler(sortHandler);

		cellTable.setRowCount(searchResult.size(), true);

		// Push the data into the widget.
		cellTable.setRowData(0, searchResult);

		// Create a Pager to control the table.
		SimplePager.Resources pagerResources = GWT
				.create(SimplePager.Resources.class);
		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,
				true);
		pager.setDisplay(cellTable);

		// Add a selection model so we can select cells.
		final SelectionModel<MyUser> selectionModel = new MultiSelectionModel<MyUser>();
		cellTable.setSelectionModel(selectionModel,
				DefaultSelectionEventManager.<MyUser> createCheckboxManager());

		// Initialize the columns.
		initTableColumns(selectionModel, sortHandler);

		// Add the CellList to the adapter in the database.
		// ContactDatabase.get().addDataDisplay(cellTable);

		// // Create the UiBinder.
		// Binder uiBinder = GWT.create(Binder.class);
		// Widget widget = uiBinder.createAndBindUi(this);
		// return widget;
	}

	/**
	 * Add the columns to the table.
	 */
	private void initTableColumns(final SelectionModel<MyUser> selectionModel,
			ListHandler<MyUser> sortHandler) {
		// Checkbox column. This table will uses a checkbox column for
		// selection.
		// Alternatively, you can call cellTable.setSelectionEnabled(true) to
		// enable
		// mouse selection.
		Column<MyUser, Boolean> checkColumn = new Column<MyUser, Boolean>(
				new CheckboxCell(true, false)) {
			@Override
			public Boolean getValue(MyUser object) {
				return selectionModel.isSelected(object);
			}
		};
		cellTable.addColumn(checkColumn, "Select");
		cellTable.setColumnWidth(checkColumn, 40, Unit.PX);

		// Name.
		Column<MyUser, String> firstNameColumn = new Column<MyUser, String>(
				new EditTextCell()) {
			@Override
			public String getValue(MyUser object) {
				return object.getName();
			}
		};
		firstNameColumn.setSortable(true);
		sortHandler.setComparator(firstNameColumn, new Comparator<MyUser>() {
			public int compare(MyUser o1, MyUser o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});
		cellTable.addColumn(firstNameColumn, "Name");
		cellTable.setColumnWidth(firstNameColumn, 20, Unit.PCT);

		// Email.
		Column<MyUser, String> emailColumn = new Column<MyUser, String>(
				new EditTextCell()) {
			@Override
			public String getValue(MyUser object) {
				return object.getEmail();
			}
		};
		emailColumn.setSortable(true);
		sortHandler.setComparator(emailColumn, new Comparator<MyUser>() {
			public int compare(MyUser o1, MyUser o2) {
				return o1.getEmail().compareTo(o2.getEmail());
			}
		});
		cellTable.addColumn(emailColumn, "Email");
		cellTable.setColumnWidth(emailColumn, 20, Unit.PCT);

	}

	@Override
	public HasClickHandlers getAddButton() {
		return addButton;
	}

//	@Override
//	public void saveToModel() {
//		SelectionModel<? super MyUser> selectionModel = cellTable
//				.getSelectionModel();
//		for (MyUser u : searchResult) {
//			if (selectionModel.isSelected(u)) {
//				user.addPendingFriendList(u);
//			}
//		}
//	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public List<MyUser> getSelectedFriends() {
		// TODO Auto-generated method stub
		List<MyUser> selected = new ArrayList<MyUser>();
		SelectionModel<? super MyUser> selectionModel = cellTable
				.getSelectionModel();
		for (MyUser u : searchResult) {
			if (selectionModel.isSelected(u)) {
				selected.add(u);
			}
		}
		return selected;
	}

}
